<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./newsList.js"></script>
    <style>
        .w {
            width: 1000px;
            margin: auto;
        }

        .tb {
            border-collapse: collapse;
            text-align: center;
        }

        .tb,
        .tb td,
        .tb th {
            border: 1px solid #ccc;
        }

        .tb th {
            width: calc(100% / 7);
        }

        .thead {
            height: 40px;
            line-height: 40px;
            background-color: #26B2F3;
            color: #fff
        }

        .badge {
            display: inline-block;
            padding: 4px 10px;
            border: 1px solid #0078BA;
            color: #0078BA;
            border-radius: 4px;
        }

        .btn {
            border: 0;
            padding: 6px 16px;
            cursor: pointer;
        }

        .btn-danger {
            border: 1px solid red;
            color: red;
            background-color: #fff;
        }

        .btn-danger:hover {
            background-color: red;
            color: #fff;
        }

        .btn-info {
            border: 1px solid #26B2F3;
            color: #26B2F3;
            background-color: #fff;
        }

        .btn-info:hover {
            background-color: #26B2F3;
            color: #fff;
        }

        .search {
            padding: 10px;
            display: flex;
        }

        .search input {
            height: 30px;
            padding-left: 10px;
            width: 300px;
            border: 1px solid #26B2F3;
            outline: none;
        }

        .search button {
            margin-left: -2px;
            background-color: #26B2F3;
            color: #fff
        }
    </style>
</head>

<body>
    <!-- <h3 class="w">要求:
        <p>
            1-动态渲染新闻列表数据
        </p>
        <p>
            2-实现删除按钮功能
        </p>
        <p>
            3-实现[根据新闻标题搜索]新闻的功能
        </p>
    </h3>
    <hr>
    <form class="search w">
        <input type="text" placeholder="请输入搜索关键词">
        <button class="btn btn-info">搜索</button>
    </form> -->
    <table class="tb w">
        <thead>
            <tr class="thead">
                <th>序号</th>
                <th>标题</th>
                <th>封面</th>
                <th>描述</th>
                <th>日期</th>
                <th>媒体</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <!-- <tr>
                <td>1</td>
                <td>新闻标题</td>
                <td>
                    <img src="http://dummyimage.com/120x120/a7f279" alt="">
                </td>
                <td>新闻标题</td>
                <td>2025-01-01</td>
                <td>
                    <div class="badge">央视网</div>
                </td>
                <td>
                    <button class="btn btn-danger">删除</button>
                </td>
            </tr> -->
        </tbody>

    </table>
    <script>
        // 获取元素对象
        var tbody = document.querySelector('.tb tbody')
        // 最终要显示的内容

        render()
        function del(index) {
            // if (!confirm('确认删除!')) {
            //     return
            // }
            // 函数内部遇到return,后续代码不在执行
            if (confirm('确认删除!')) {
            }
            newsList.splice(index, 1) //将新闻数据从newsList中删除
            render()  //更新视图
        }

        function render() {
            var html = ''
            // item: 系统参数, 表示数组元素
            newsList.forEach(function (item,index) {
                html += '<tr><td>' + item.id + '</td>'
                html += '<td>' + item.title + '</td>'
                html += '<td><img src="' + item.img + '"></td>'
                html += '<td>' + item.description.slice(0, 20) + '</td>'
                html += '<td>' + item.date + '</td>'
                html += '<td>' + item.media + '</td>'
                html += '<td><button onclick =" del(' + index + ')">删除</button></td>'
                html += '</tr>'
            })
            // 给tbody元素设置内容
            tbody.innerHTML = html
        }

    </script>
</body>

</html>